package com.hbut.system.service.question.imp;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.hbut.common.core.constants.Constants;
import com.hbut.common.core.enums.ResultCode;
import com.hbut.common.security.exception.ServiceException;
import com.hbut.system.domain.question.Question;
import com.hbut.system.domain.question.dto.QuestionAddDTO;
import com.hbut.system.domain.question.dto.QuestionEditDTO;
import com.hbut.system.domain.question.dto.QuestionQueryDTO;
import com.hbut.system.domain.question.es.QuestionES;
import com.hbut.system.domain.question.vo.QuestionDetailVO;
import com.hbut.system.domain.question.vo.QuestionVO;
import com.hbut.system.elasticsearch.QuestionRepository;
import com.hbut.system.mapper.question.QuestionMapper;
import com.hbut.system.service.question.IQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@RefreshScope

public class IQuestionServiceImp implements IQuestionService {
    @Autowired
    QuestionMapper questionMapper;

    @Autowired
    QuestionRepository questionRepository;

    @Override
    public List<QuestionVO> list(QuestionQueryDTO questionQueryDTO) {
        String excludeIdStr = questionQueryDTO.getExcludeIdStr();
        if (StrUtil.isNotEmpty(excludeIdStr)){
            String[] ids = excludeIdStr.split(Constants.EXCLUDED_SEM);
            Set<Long> collect = Arrays.stream(ids).map(Long::valueOf).collect(Collectors.toSet());
            questionQueryDTO.setExcludeIdSet(collect);
        }
        //分页插件的使用
        PageHelper.startPage(questionQueryDTO.getPageNum(), questionQueryDTO.getPageSize());
       return questionMapper.selectQuestionList(questionQueryDTO);
    }

    @Override
    public boolean add(QuestionAddDTO questionAddDTO) {
        List<Question> questions = questionMapper.selectList(new LambdaQueryWrapper<Question>().eq(Question::getTitle, questionAddDTO.getTitle()));
        if (CollectionUtil.isNotEmpty(questions)){
            throw new ServiceException(ResultCode.FAILED_ALREADY_EXISTS);
        }
        Question question = new Question();
        BeanUtil.copyProperties(questionAddDTO,question);
        int insert = questionMapper.insert(question);
        if(insert <= 0){
            return false;
        }
        QuestionES questionES = new QuestionES();
        BeanUtil.copyProperties(question,questionES);
        questionRepository.save(questionES);
        return true;
    }

    @Override
    public QuestionDetailVO detail(Long questionId) {
        Question question = getQuestionById(questionId);
        QuestionDetailVO questionDetailVO = new QuestionDetailVO();
        BeanUtil.copyProperties(question,questionDetailVO);
        return questionDetailVO;
    }

    @Override
    public int edit(QuestionEditDTO questionEditDTO) {
        Question question = getQuestionById(questionEditDTO.getQuestionId());
        BeanUtil.copyProperties(questionEditDTO,question);
        QuestionES questionES = new QuestionES();
        BeanUtil.copyProperties(question,questionES);
        questionRepository.save(questionES);
        return questionMapper.updateById(question);
    }

    @Override
    public int deleteById(Long questionId) {
        Question question = getQuestionById(questionId);
        questionRepository.deleteById(questionId);
        return questionMapper.deleteById(question);
    }

    public Question getQuestionById(Long questionId){
        Question question = questionMapper.selectById(questionId);
        if(question == null){
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        return question;
    }

}
